package team.stride.tabs.records;

import java.util.List;

import team.stride.R;
import team.stride.database.StrideDB;
import team.stride.utils.UtilsStride;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MapListViewItemAdapter extends BaseAdapter/*
														 * implements
														 * OnClickListener
														 */
{
	private Context context;

	private List<MapListViewItem> mapslist;

	public MapListViewItemAdapter(Context context, List<MapListViewItem> mapslist)
	{
		this.context = context;
		this.mapslist = mapslist;
	}

	public int getCount()
	{
		return mapslist.size();
	}

	public MapListViewItem getItem(int position)
	{
		return mapslist.get(position);
	}

	public long getItemId(int position)
	{
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup viewGroup)
	{
		MapListViewItem entry = mapslist.get(position);
		if (convertView == null)
		{
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.map_row, null);
		}
		TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
		tvName.setText(entry.getName());

		TextView tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
		tvUserName.setText(entry.getUserName());

		TextView tvLength = (TextView) convertView.findViewById(R.id.tvLength);
		tvLength.setText("" + entry.getLength());

		TextView tvTime = (TextView) convertView.findViewById(R.id.tvTime);
		tvTime.setText("" + entry.getTime());

		TextView tvSpeed = (TextView) convertView.findViewById(R.id.tvSpeed);
		tvSpeed.setText("" + entry.getTopSpeed());

		TextView tvLaps = (TextView) convertView.findViewById(R.id.tvLaps);
		tvLaps.setText("" + entry.getLaps());

		ImageView ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
		ivImg.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				MapListViewItem element = mapslist.remove(position);
				notifyDataSetChanged();
				UtilsStride.removeFromDB(StrideDB.MATCH_R, element);
			}
		});
		// ivImg.setBackgroundColor(0xF0F020);
		// ivImg.setImageResource(R.drawable.omnomnom);

		/*
		 * // Set the onClick Listener on this button Button btnRemove =
		 * (Button) convertView.findViewById(R.id.btnRemove);
		 * //btnRemove.setOnClickListener(this);
		 * btnRemove.setOnClickListener( new OnClickListener() { public void
		 * onClick(View v) { // Perform action on clicks MapListViewItem entry =
		 * (MapListViewItem) v.getTag(); mapslist.remove(entry); //
		 * listPhonebook.remove(view.getId()); notifyDataSetChanged(); } } );
		 * // Set the entry, so that you can capture which item was clicked and
		 * // then remove it // As an alternative, you can use the id/position
		 * of the item to capture // the item // that was clicked.
		 * btnRemove.setTag(entry);
		 * // btnRemove.setId(position);
		 */

		return convertView;
	}
	/*
	 * @Override public void onClick(View view) { MapListViewItem entry =
	 * (MapListViewItem) view.getTag(); mapslist.remove(entry); //
	 * listPhonebook.remove(view.getId()); notifyDataSetChanged(); }
	 */
}
